<?php

/**
 * O2O团购PC版本
 * ============================================================================
 * * 版权所有 2009-2016 郑州司科计算机科技有限公司，并保留所有权利。
 * 购买网址: http://www.jzphp.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
  * 咨询电话：400-823-0371 QQ：313999875
 * ============================================================================
 * $Author: zzsike $
 * $Time 2016-02-03 sammy $
*/


require_once(dirname(dirname(__FILE__)) . '/app.php');

$gid = abs(intval($_GET['gid']));
$sid = abs(intval($_GET['sid']));

$daytime = time();
$condition = array( 
		'team_type' => 'seconds',
		"begin_time<=$daytime",
		"end_time>=$daytime",
		);
$city_id = abs(intval($city['id']));
$condition[] = "(city_ids like '%@{$city_id}@%' or city_ids like '%@0@%') or (city_ids = '' and city_id in(0,{$city_id}))";

/* filter */
if($gid){
  $condition['group_id'] = $gid;
}
if($sid){
  $condition['sub_id'] = $sid;
}
if($gid){
   $cates = DB::LimitQuery('category', array('condition'=>array(
		  'zone'=>'group', 
		  'fid'=>$gid, 
		  'display'=>'Y',
		  ), 
		  'order'=>'ORDER BY `sort_order` DESC, `id` DESC'
	));
	for($i = 0;$i<count($cates);$i++){
	  $cates[$i]['number'] = Table::Count('team', array('team_type'=>'seconds', 'sub_id'=>$cates[$i][id], "(city_ids like '%@{$city[id]}@%' or city_ids like '%@0@%') or (city_id in(0,{$city[id]}))","begin_time < '{$daytime}'", "end_time > '{$daytime}'"));
	}
}else{
	 $cates = DB::LimitQuery('category', array('condition'=>array(
		  'zone'=>'group', 
		  'fid'=>0, 
		  'display'=>'Y',
		  ), 
		  'order'=>'ORDER BY `sort_order` DESC, `id` DESC'
	));
    for($i = 0;$i<count($cates);$i++){
	  $cates[$i]['number'] = Table::Count('team', array('team_type'=>'seconds', 'group_id'=>$cates[$i][id],"(city_ids like '%@{$city[id]}@%' or city_ids like '%@0@%') or (city_id in(0,{$city[id]}))", "begin_time < '{$daytime}'", "end_time > '{$daytime}'"));
	}
}

$count = Table::Count('team', $condition);
list($pagesize, $offset, $pagestring) = pagestring($count, 10);
$teams = DB::LimitQuery('team', array(
	'condition' => $condition,
	'order' => 'ORDER BY begin_time DESC, id DESC',
	'size' => $pagesize,
	'offset' => $offset,
));
foreach($teams AS $id=>$one){
	team_state($one);
	if ($one['state']=='none') $one['picclass'] = 'isopen';
	if ($one['state']=='soldout') $one['picclass'] = 'soldout';
	$teams[$id] = $one;
}

$pagetitle = '秒杀抢团';
include template('team_seconds');

function current_teamcategory($gid='0') {
	global $city;
	$a = array(
			'/team/seconds.php' => '所有',
			);
    $categorys = DB::LimitQuery('category', array(
		'condition' => array( 'zone' => 'group','fid' => '0','display' => 'Y' ),
		'order' => 'ORDER BY sort_order DESC, id DESC',
	));
	$categorys = Utility::OptionArray($categorys, 'id', 'name');
	foreach($categorys AS $id=>$name) {
		$a["/team/seconds.php?gid={$id}"] = $name;
	}
	$l = "/team/seconds.php?gid={$gid}";
	if (!$gid) $l = "/team/seconds.php";
	return current_link($l, $a, true);
}
